package com.chuangjiangx.member.manager.client.web.stored.controller;

import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.client.web.stored.request.QueryMbrOrderRequest;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.common.utils.StrUtils;
import com.chuangjiangx.member.stored.dao.model.InMbrOrder;
import com.chuangjiangx.member.stored.ddd.dal.condition.MbrOrderDetailCondition;
import com.chuangjiangx.member.stored.ddd.dal.condition.MbrOrderListCondition;
import com.chuangjiangx.member.stored.ddd.dal.dto.MbrOrderDetail;
import com.chuangjiangx.member.stored.ddd.domain.exception.MemberPaymentException;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrOrderType;
import com.chuangjiangx.member.stored.ddd.query.MbrOrderQuery;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cj-ch
 * @date 2018/6/15 上午9:54
 */
@Slf4j
@RestController
@Api(tags = "订单" , value = "订单相关")
@RequestMapping(value = "/mbr/order",produces = MediaType.APPLICATION_JSON_VALUE)
public class MbrOrderClientController {

    @Autowired
    private MbrOrderQuery mbrOrderQuery;

    @RequestMapping(value = "/consumer-list",method = {RequestMethod.GET,RequestMethod.POST})
    @Login
    @ApiOperation(value = "消费列表")
    public CamelResponse<PagingResult<InMbrOrder>> list(@Validated QueryMbrOrderRequest orderRequest){
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        MbrOrderListCondition condition = new MbrOrderListCondition();
        BeanUtils.copyProperties(orderRequest,condition);
        condition.setType(MbrOrderType.CONSUME.value);
        condition.setMerchantId(user.getMerchantId());
        condition.setStoreId(orderRequest.getStoreId());
        condition.setStoreUserName(orderRequest.getStoreUserName());
        PagingResult<InMbrOrder> result = mbrOrderQuery.appMbrOrderList(condition);
        return (CamelResponse<PagingResult<InMbrOrder>>)ResponseUtils.successCamel(result);
    }

    /**
     * 订单详情
     * @param orderNumber 会员订单编号
     * @return
     */
    @RequestMapping(value = "/detail/{orderNumber}",method = {RequestMethod.GET,RequestMethod.POST})
    @Login
    @ApiOperation(value = " 订单详情")
    public CamelResponse<MbrOrderDetail> detail(@PathVariable @ApiParam(value = "订单编号") String orderNumber){
        MbrOrderDetailCondition mbrOrderDetailCondition = new MbrOrderDetailCondition();
        mbrOrderDetailCondition.setOrderNumber(orderNumber);
        MbrOrderDetail detail = mbrOrderQuery.aiCashierMbrOrderDetail(mbrOrderDetailCondition);
        if(null == detail) {
            throw new MemberPaymentException("", "查询订单不存在");
        }
        detail.setMobile(StrUtils.encodeMobilePhone(detail.getMobile()));
        return (CamelResponse<MbrOrderDetail>)ResponseUtils.successCamel(detail);
    }
}
